package com.base.cn.platform.os.controller.user.integral;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.user.integral.CusUserIntegralRule;
import com.base.cn.platform.os.entity.user.integral.IntegralRule;
import com.base.cn.platform.os.service.user.integral.CusUserIntegralRuleBiz;
import com.base.cn.platform.os.service.user.integral.IntegralRuleBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Data 用户积分规则
 * @ClassName  CusUserIntegralRuleController
 * @author :ll
 * @Create Date : 2018年4月24日
 */
@RestController
@RequestMapping("/manage/user")
public class CusUserIntegralRuleController extends BaseController {

    @Autowired
    private CusUserIntegralRuleBiz cusUserIntegralRuleBiz;
    @Autowired
    private IntegralRuleBiz integralRuleBiz;



    /**
     * 搜索经验规则明细列表
     * @param cusUserIntegralRule
     * @return
     */
    @RequestMapping("/queryAjaxUserIntegralRuleList")
    public PageInfo<CusUserIntegralRule> queryAjaxUserIntegralRuleList(@RequestBody CusUserIntegralRule cusUserIntegralRule ,
                                                  @RequestParam(value = "currentPage",required = false,defaultValue = "1")Integer currentPage){
        Pagination page =new Pagination();
        page.setCurrentPage(currentPage);
        return cusUserIntegralRuleBiz.queryCusUserExpRulePage(cusUserIntegralRule,page);
    }



    /**
     * 添加修改方法只能修改分数
     * @param userIntegralRule
     * @return
     */
    @RequestMapping("/saveUserIntegralRule")
    public Map<String ,Object> saveUserIntegralRule(@RequestBody CusUserIntegralRule userIntegralRule){
        Map<String,Object>  checkMap = saveCheckUserIntegralRule(userIntegralRule);
        if (ObjectUtils.isNotEmpty(checkMap)){
            return checkMap;
        }
        CusUserIntegralRule cusUserIntegralRule =cusUserIntegralRuleBiz.saveUpdateCusUserIntegralRule(userIntegralRule);
        if(ObjectUtils.isNotEmpty(cusUserIntegralRule)){
            if (DataUtil.idIsNotNull(cusUserIntegralRule.getId())){
                return ResultUtil.SUCCESS("修改成功",cusUserIntegralRule);
            }else{
                return ResultUtil.SUCCESS("添加成功");
            }
        }else{
            return ResultUtil.ERROR("系统繁忙请稍后再试");
        }
    }


    /**
     * 修改积分规则状态
     * @param integralRuleId
     * @param status
     * @return
     */
    @RequestMapping("/updateUserIntegralRuleStatus")
    public Map<String,Object> updateUserIntegralRuleStatus(@RequestParam("integralRuleId")BigDecimal integralRuleId,
                                                           @RequestParam("status") Integer status){
       CusUserIntegralRule cusUserIntegralRule = cusUserIntegralRuleBiz.updateUserIntegralRuleStatus(integralRuleId,status);
       String  message ="";
       if(status==1){
           message="恢复成功";
       }else if (status==2){
           message="封禁成功";
       }
       return ResultUtil.SUCCESS(message,cusUserIntegralRule);
    }

    //=====================================================================================
    /**
     * 添加修改方法
     * @param userIntegralRule
     * @return
     */
    private Map<String,Object> saveCheckUserIntegralRule(CusUserIntegralRule userIntegralRule){
        if(!DataUtil.idIsNotNull(userIntegralRule.getId())){
            if(StringUtils.isEmpty(userIntegralRule.getAction())){
                return ResultUtil.ERROR("请填写行为说明");
            }else if(userIntegralRule.getType()==null||userIntegralRule.getType()<=0){
                return ResultUtil.ERROR("请填写获取积分类型");
            }else if(userIntegralRule.getType()!=3&&(userIntegralRule.getScore()==null||userIntegralRule.getScore()<=0)){
                return ResultUtil.ERROR("请填写获取积分数");
            }else if(StringUtils.isEmpty(userIntegralRule.getTermDepict())){
                return  ResultUtil.ERROR("请填写积分描述");
            }
        }
        if(DataUtil.idIsNotNull(userIntegralRule.getId())){
            if(userIntegralRule.getScore()==null||userIntegralRule.getScore()<=0){
                return ResultUtil.ERROR("请填写获取积分数");
            }else if(!StringUtils.isNumber(userIntegralRule.getScore().toString())) {
                return ResultUtil.ERROR("分数为不为0的正整数");
            }
        }
        return null;
    }


    @RequestMapping("addIntegralRule")
    @ResponseBody
    public Map<String, Object> addIntegralRule(@RequestBody IntegralRule integralRule) {
        integralRuleBiz.save(integralRule);
        return ResultUtil.SUCCESS();
    }


    /**
     * 获取全部积分规则
     * @return
     */
    @RequestMapping("/integralRuleData")
    public Map<String, IntegralRule> integralRuleData(@RequestParam("type")String type){
        return integralRuleBiz.integralRuleData(type);
    }




    /**
     * 更新积分规则
     * @param
     * @return
     */
    @RequestMapping("/updateIntegralRuleConfig")
    @ResponseBody
    Map<String, Object> updateIntegralRuleConfig(@RequestBody Map<String,Object> data){
        IntegralRule integralRule = integralRuleBiz.findOne("`key`='" + data.get("key") + "'", null);
        String value = null;
        if (integralRule.getIsList()) {
            Map<String, Object> map = new HashMap<>();
            map.put("list",data.get("content"));
            value=gson.toJson(map);
        }else{
            value = gson.toJson(data.get("content"));
        }
        integralRule.setValue(value);
        integralRule.setKey(data.get("key").toString());
        integralRuleBiz.updateById(integralRule,"key");

        return ResultUtil.SUCCESS();
    }


    /**
     * 获取积分规则
     * @param
     * @return
     */
    @RequestMapping("/getUserIntegralRuleByKey")
    @ResponseBody
   public IntegralRule getUserIntegralRuleByKey(@RequestParam String actionKey){
        return  integralRuleBiz.getIntegralRuleByKey(actionKey);
    }


}
